function
is_msie_6()
    {
    return $.browser.msie && $.jqbrowser.version.number() < 7;
    }
function
compose_node_id(prefix, component, section, subsection)
    {
    return String(prefix) + "___" + "c_" + String(component) + "_s_" + String(section) + (typeof(subsection) != "undefined" ? ("_su_" + String(subsection)) : "" );
    }

function
replace_text(string, source, replacement)
    {
    return String(string).replace(new RegExp(source, "ig"), replacement);
    }

function
toggle_subsection_tab(component, section, subsection, oldstate, newstate)
    {
    var subsection_tab_element = $(compose_node_id("#subsection_tab", component, section, subsection));
    subsection_tab_element.addClass("tab_" + newstate).removeClass("tab_" + oldstate);
    subsection_tab_element.css("background-image", replace_text(subsection_tab_element.css("background-image"), "_" + oldstate, "_" + newstate));
    subsection_tab_element.find("img").each
        ( function(i)
            {
            if (is_msie_6())
                this.src = replace_text(this.realSrc, "_" + oldstate, "_" + newstate);
            else
                this.src = replace_text(this.src, "_" + oldstate, "_" + newstate);
            }
        );
    subsection_tab_element.end();
    }

function
ep_tabs_show(component, section, subsection)
    {
    var active_subsection_id_element = $("input[@name=" + compose_node_id("active_subsection_id", component, section) + "]");
    var prev_subsection = active_subsection_id_element.val();
    if ( subsection != prev_subsection )
        {
        // switch subsection tabs
        toggle_subsection_tab(component, section, prev_subsection, "active", "inactive");
        toggle_subsection_tab(component, section, subsection, "inactive", "active");
        // switch subsection content
        $(compose_node_id("#subsection", component, section, prev_subsection)).addClass("tab_inactive").removeClass("tab_active").end();
        $(compose_node_id("#subsection", component, section, subsection)).removeClass("tab_inactive").addClass("tab_active").end();
        active_subsection_id_element.val(subsection).end();
        }
    }


function
fix_tab_forms_for_gecko()
    {
    // hide forms
    $("//fieldset").hide().end();

    // processing
    $("//fieldset").find("li/label").each
        ( function(i)
            {
            var label_content = this.innerHTML;
            var label_width = document.defaultView.getComputedStyle(this, "").getPropertyValue("width");
            var label_span = document.createElement("span");
            label_span.style.display = "block";
            label_span.style.width = label_width;
            label_span.innerHTML = label_content;
            this.style.display = '-moz-inline-box';
            this.innerHTML = null;
            this.appendChild(label_span);
            }
        ).end();
    // show forms
    $("//fieldset").show().end();
    }

function
enable_ep_menu_on_document_ready()
    {
    $( function()
            {
            $("#ep_workspace").css("margin-left", "220px").end();
            $("#ep_menu").show().end();
            }
        );
    }

// global code
$( function()
        {
        if ( $.browser.mozilla && !$.browser.opera )
          fix_tab_forms_for_gecko();

        ep_update_active_subsections();
        ep_update_session_timeout();
        }
    );

function
ep_update_session_timeout()
    {
    $("span#ep_session_timeout")
        .attr("style", session_duration <= 15 ? "color:red;" : "")
        .html(session_duration ? session_duration + " minute" + ( session_duration > 1 ? "s" : "" ) + " left" : "Session has expired")
        .end();
    session_duration--;

    if (session_duration >= 0)
        setTimeout(ep_update_session_timeout, 60000);
    }

function
ep_update_active_subsections()
    {
    $(".ep_subsection_tabpane").each
        ( function(i)
            {
            var str = String( $(this).find(":first-child").attr("id") );
            str.match(/subsection_tab___c_(\d+)_s_(\d+)/ig);
            // get component and section values
            var component = RegExp.$1;
            var section = RegExp.$2;
            // get value of active_subsection_id for this section
            var active_subsection_id_element = $("input[@name=" + compose_node_id("active_subsection_id", component, section) + "]");
            str = String( $(this).find(".tab_active").attr("id") );
            str.match(/subsection_tab___c_\d+_s_\d+_su_(\d+)/ig);
            var de_facto_active_subsection_id = RegExp.$1;
            var target_active_subsection_id = active_subsection_id_element.val();

            if ( de_facto_active_subsection_id != target_active_subsection_id )
                {
                active_subsection_id_element.val(de_facto_active_subsection_id);
                ep_tabs_show(component, section, target_active_subsection_id);
                }
            }
        );
    }

// this stuff will eventually go to the right place

// will log the user off and reload the interface 
// takes the prompt as an argument, if prompt is empty, will use the default prompt
function
ep_log_out(username)
    {
    var log_out_confirmed = confirm("You are currently logged on as " + username + ". Are you sure you want to log out?" );
 
    if (log_out_confirmed)
        {
        try {
            $("#ep_active_component_id").val("").end();
            $("#ep_render_component_ids").val(0).end();
            $("#ep_kill_session").val(getCookie("ep_session_id")).end();
            $("form#ep_main_form").submit();
            }
        catch (exc)
            {
            alert ("Expression Profiler was unable to log you out gracefully. Please close your browser window.");
            }
        }
    }

function
ep_submit_component(id, target_id)
    {
    $("#ep_active_component_id").val(id).end();
    eval( $("#ep_targets___c_" + String(id)).val(target_id).end() );
    ep_display_status_window();
    $("form#ep_main_form").submit();
    }

function
ep_display_status_window(is_not_first_call)
    {
    $("#ep_status_text").text("Please wait...");
    $("#ep_status_window").show();
    $.get
        ( cgi_href + "/ep_status.pl"
        /*, { session_id: escape(session_id), first_time: is_not_first_call ? "0" : "1" }*/
        , function(status_text)
            {
            $("#ep_status_text").text(status_text ? status_text : "Please wait...");
            setTimeout( "ep_display_status_window(1)", 1500 );
            }
        );
    }
